<?php

/**************************************************************
File Name		: exportData.php
Purpose			: Exports Tables and Data
Author			: Susheel Deshmukh
Create Date		: 08/19/2002
Last Modified	: 08/19/2002
***************************************************************/

// Files included from dbutility
include_once("global_conf.php");
include_once("lib/cronExportData.php");
include_once("lib/main.php");
// Ends here

$db_name = "mki-crm";
$database_name=$db_name;
$save_data="on";
$save_tables="on";

// set the timelimit
//set_time_limit($CONF['time_limit']);
set_time_limit(120);

if($db_name != "") {
	$mode="shell";
	$database_name=$db_name;
	$save_data="on";
	$save_tables="on";

}
$phpMyBackup=new phpMyBackup;

if($database_name != "") {
	delOldBackups();
	$exportLib=new dropTable;
	$phpMyBackup->sqlConnect($CONF['sql_host'],$CONF['sql_usr'],$CONF['sql_pass']);
	if($mode=="web") {
		$db_list[]=$database_name;
	} else {
		 $db_list=explode(",",$database_name);
	}
	foreach($db_list as $export_db) {
	if(!mysql_select_db($export_db,$con)) {
		echo"ERROR: Could not select db $export_db<br><br>\n\n";
	} else {
	$exportLib->dbName=$export_db;
	$fileData=$exportLib->structure();
	$today = getdate();
	$month = $today[mon];
	if($month < 10)
	{
		$month = "0".$month;
	}
	$day = $today[mday];
	if($day < 10)
	{
		$day = "0".$day;
	}
	$year = $today[year];
	$hours = $today[hours];
	if($hours < 10)
	{
		$hours = "0".$hours;
	}
	$min = $today[minutes];
	if($min < 10)
	{
		$min = "0".$min;
	}
	$time=$day.$month.$year.$hours.$min;
	//$time = date("mdYHi",$time);
	// see if zlib is installed
	if (function_exists('gzopen')) {
		// open file for writing with maximum compression
		$zp = gzopen("./export/".$exportLib->dbName."_".$time.".sql.gz", "w9");
		gzwrite($zp, $fileData);
		gzclose($zp);
		$BackupFileName=$exportLib->dbName."_".$time.".sql.gz";
		$BackupIsGziped=1;
	} else {
		// write file old fasion way
		$fp=fopen("./export/".$exportLib->dbName."_".$time.".sql", "w");
		fwrite($fp,$exportLib->structure());
		fclose($fp);
		$BackupIsGziped=0;
		$BackupFileName=$exportLib->dbName."_".$time.".sql";
	}

	$backup_size=strlen($fileData);
	if(isset($save_data) && $save_data=="on") {
		$store_data="yes";
	} else {
		$store_data="no";
	}

	if(isset($save_tables) && $save_tables=="on") {
		$store_tables="yes";
	} else {
		$store_tables="no";
	}

	$fp=fopen("./export/".$exportLib->dbName."_".$time.".info", "w");
	fwrite($fp,"$time|$export_db|$backup_size|$store_tables|$store_data|$BackupIsGziped|$comments");
	fclose($fp);

	chmod("./export/".$BackupFileName,0777);
	chmod("./export/".$exportLib->dbName."_".$time.".info",0777);


	if($CONF['ftp_use']==1 && function_exists('ftp_connect')) {
		$ftp=ftp_store($BackupFileName);
		$ftp .= ftp_store($exportLib->dbName."_".$time.".info");
	}
	}
	}

}
@mysql_close($con);
?>
